//
//  QNNumberPadModel.m
//  CreditCat
//
//  Created by rsh on 20120/8/27.
//  Copyright © 2020年 rsh. All rights reserved.
//

#import "QNNumberPadModel.h"

@interface QNNumberPadModel()

@property (copy, nonatomic, readwrite) NSString *keybordNumber;
@property (copy, nonatomic, readwrite) NSString *secretNumberStr;//加密规则需要制定

@end

@implementation QNNumberPadModel

- (instancetype)initWithKeybordNumber:(NSString *)keybordNumber{
    if (self = [super init]) {
        self.keybordNumber = keybordNumber;
    }
    return self;
}

- (NSString *)secretNumberStr{
    return self.keybordNumber;
}

@end



@implementation UIColor (Encode)

/**
 Hex转UIColor
 
 @param hexString Hex字符串
 @return UIColor
 */
+ (UIColor *)colorWithHexString:(NSString *)hexString {
    return [UIColor colorWithHexString:hexString alpha:1.0];
}


/**
 Hex转UIColor
 
 @param hexString Hex字符串
 @param alphaValue 透明度
 @return UIColor
 */
+ (UIColor *)colorWithHexString:(NSString *)hexString alpha:(CGFloat)alphaValue
{
    NSString *cString = [[hexString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    // String should be 6 or 8 characters
    if ([cString length] < 6) {
        return [UIColor clearColor];
    }
    
    // 判断前缀并剪切掉
    if ([cString hasPrefix:@"0X"])
        cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"])
        cString = [cString substringFromIndex:1];
    if ([cString length] != 6)
        return [UIColor clearColor];
    
    // 从六位数值中找到RGB对应的位数并转换
    NSRange range;
    range.location = 0;
    range.length = 2;
    
    //R、G、B
    NSString *rString = [cString substringWithRange:range];
    
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:alphaValue];
}


@end


@implementation UIImage (Encode)

/**
 读取私有库中图片资源
 
 @param cls 私有库中Class类名, 这个class不能是Category的class
 @param podName 私有库名称 s.name
 @param imgName 图片名称
 @return 图片
 */
+ (UIImage *)qn_imgWithCls:(Class)cls podName:(NSString *)podName imgName:(NSString *)imgName{
    
    if (!cls || podName.length <= 0 || imgName.length <= 0) {
        return nil;
    }
    
    Class targetCls = cls;
    //    NSClassFromString(className);
    // 源码所在的bundle
    NSBundle *clsBundle = [NSBundle bundleForClass:targetCls];
    NSLog(@"clsBundle = %@ \t\n",clsBundle);
    if (!clsBundle) {
        return nil;
    }
    // 资源所在的bundle路径
    NSURL *resUrl = [clsBundle URLForResource:podName withExtension:@"bundle"];
    if (!resUrl) {
        return nil;
    }
    NSBundle *resBundle = [NSBundle bundleWithURL:resUrl];
    NSLog(@"resBundle = %@ \t\n",resBundle);
    if (!resBundle.loaded) {
        [resBundle load];
    }
    UIImage *retImg = [UIImage imageNamed:imgName inBundle:resBundle compatibleWithTraitCollection:nil];
    
    return retImg;
}

@end
